//
//  BSTabBarController.m
//  BaiSiBuDeJie
//
//  Created by 蔡一凡 on 8/22/16.
//  Copyright © 2016 tsaievan. All rights reserved.
//

#import "BSTabBarController.h"
#import "BSAddNavController.h"
#import "BSFocusNavController.h"
#import "CCKMeNavController.h"
#import "BSAddViewController.h"
#import "BSTabBarModel.h"
#import "BSAddView.h"
#import "SBRealFocusVc.h"
#import "BSLatestViewController.h"
#import "EssenceNavigationController.h"
#import "BSWordViewController.h"
#import "HMTabBar.h"
#import "BSImageViewController.h"
#import "BSAddVideoController.h"
#import "BSAddPathViewController.h"

@interface BSTabBarController ()<BSAddViewDelegate,HMTabBarDelegate>

@end

@implementation BSTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addRedBtn];
    

}

- (void)addChildViewControllers
{
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
    [[BSTabBarModel tabBarModels] enumerateObjectsUsingBlock:^(BSTabBarModel *tabBarModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        BSNavigationController *nav;
        
        if([tabBarModel.navigationController isEqualToString: @"BSFocusNavController"]){
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"SBRealFocusVc" bundle:nil];
            SBRealFocusVc *focusVc = [sb instantiateInitialViewController];
            
            nav= [[NSClassFromString(tabBarModel.navigationController) alloc]initWithRootViewController:focusVc];
            
        }
        else if ([tabBarModel.navigationController isEqualToString: @"BSLatestNavControllerViewController"])
        {
            UIStoryboard *latestSb = [UIStoryboard storyboardWithName:@"BSLatest" bundle:nil];
            
            BSLatestViewController *latestVc = [latestSb instantiateInitialViewController];
            
            nav = [[NSClassFromString(tabBarModel.navigationController) alloc] initWithRootViewController:latestVc];
        }
        else if([tabBarModel.navigationController isEqualToString: @"EssenceNavigationController"])
        {
            UIStoryboard *essenceStory = [UIStoryboard storyboardWithName:@"essence" bundle:nil];
            
            EssenceNavigationController *essenceNav = [essenceStory instantiateInitialViewController];
            nav= essenceNav;
        }
        else
        {
            
            nav= [[NSClassFromString(tabBarModel.navigationController) alloc]initWithRootViewController:[[NSClassFromString(tabBarModel.viewController) alloc]init]];
        }
        
        //        nav.title = tabBarModel.title;
        NSDictionary *textSelectedAttributes = @{NSForegroundColorAttributeName : [UIColor colorWithRed:0.26 green:0.26 blue:0.26 alpha:1.00] };
        [nav.tabBarItem setTitleTextAttributes:textSelectedAttributes forState:UIControlStateSelected];
        NSDictionary *textNormalAttributes = @{NSForegroundColorAttributeName :[UIColor colorWithRed:0.55 green:0.51 blue:0.50 alpha:1.00]
                                               };
        [nav.tabBarItem setTitleTextAttributes:textNormalAttributes forState:UIControlStateNormal];
        NSLog(@"%@",tabBarModel);
        [self addChildViewController:nav];
        
    }];
    
    [self.view layoutIfNeeded];
    
    HMTabBar *tabBar = [HMTabBar tabBarWithTabs:[BSTabBarModel tabBarModels]];
    tabBar.frame = self.tabBar.bounds;
    tabBar.delegate = self;
    [self.tabBar addSubview:tabBar];
    //    tabBar.backgroundColor = [UIColor redColor];
    
    NSLog(@"%s",__FUNCTION__);
}

-(void)addRedBtn
{
    [self addChildViewControllers];
}

#pragma mark *** HMTabBarDelegate代理方法 ***
-(void)didTabButtonClicked:(UIButton *)selectedButton
{
    if(selectedButton.tag == 2)
    {
        [self didClicBtn];
        
        selectedButton.highlighted = NO;
    }
    else
    {
        self.selectedIndex = selectedButton.tag;
    }
}

-(void)didClicBtn
{
    
    BSAddView * addView = [BSAddView BSAddView];
    
    addView.frame = self.view.bounds;
    
    addView.delegate = self;
    
    [self.view addSubview:addView];
    
    
}

-(void)BSAddViewDelegateWith:(NSInteger)tag
{
    
    UINavigationController * navigationController = self.viewControllers[self.selectedIndex];
    
    if(tag == 0)
    {
//        BSAddVideoController * addViewC = [[BSAddVideoController alloc]init];
        
        
        
//        [navigationController.viewControllers.lastObject presentViewController:addViewC animated:YES completion:^{
//            
//            NSLog(@"--------");
//        }];
    }
    
    if(tag == 1)
    {
        BSImageViewController * imageViewC = [[BSImageViewController alloc]init];
        
        [navigationController.viewControllers.lastObject presentViewController:imageViewC animated:YES completion:^{
            
        }];
        
    }
    
    if(tag == 2)
    {
        BSWordViewController * wordViewController = [BSWordViewController wordViewController];
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [navigationController pushViewController:wordViewController animated:YES];
        });
    }
    
    if(tag == 5)
    {
        BSAddPathViewController * PathViewController = [BSAddPathViewController addPathViewController];
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [navigationController pushViewController:PathViewController animated:YES];
        });
    }
}
@end
